def maxProfit(prices):
    if not prices:
        return 0
    res = 0

    profit = [[0 for i in range(3)] for i in range(len(prices))]

    '''
    依次是，1.没有买入股票，
           2.买入1股票，
           3.之前买入，现在卖掉的
    '''
    profit[0][0], profit[0][1], profit[0][2] = 0, -prices[0], 0

    for i in range(1, len(prices)):
        print("第", i, "天开始：", profit)
        profit[i][0] = profit[i - 1][0]
        profit[i][1] = max(profit[i - 1][1], profit[i - 1][0] - prices[i])  # 当天买入，要减去利润
        profit[i][2] = profit[i - 1][1] + prices[i]  # 当天卖出，加入利润
        res = max(res, profit[i][0], profit[i][1], profit[i][2])
        print("第", i, "天结束：", profit)
    return res


if __name__ == '__main__':
    print(maxProfit([7, 1, 5, 3, 6, 4]))
